﻿/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 6/29/2009
 * Time: 3:10 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;

namespace QuickReplaceContent
{
	class Program
	{
		public static void Main(string[] args)
		{
			if(args.Length >= 4){
				string srcContent = readFile(args[0]);
				string desContent = readFile(args[1]);
				string fromStr	  = convertSpecialChar(args[2]);
				string toStr	  = convertSpecialChar(args[3]);
				
				
				string replacedStr = subString( desContent, fromStr, toStr, 0 );
				if(replacedStr != null || replacedStr.Length > 0){
					desContent = desContent.Replace( replacedStr, fromStr + srcContent + toStr );
					writeFile(args[1], desContent);				
				}
			}
			else if(args.Length >= 3){
				string srcContent = readFile(args[0]);
				string fromStr	  = convertSpecialChar(args[1]);
				string toStr	  = convertSpecialChar(args[2]);
				
				writeFile( args[0],  readFile(args[0]).Replace(fromStr,toStr) );
				
			}else{
				
				
				Console.WriteLine("EXE SourceContentFile DestinationFile FirstString LastString");
				Console.WriteLine("Copy whole content of [SourceContentFile] to [DestinationFile] but within the first and last string.");
				Console.WriteLine("Eg: Source: GAME");
				Console.WriteLine("    Destination: BBBBB1TOM2BBBB");
				Console.WriteLine("    FirstString: 1");
				Console.WriteLine("    LastString: 2");
				Console.WriteLine("    Result: BBBBB1GAME2BBBB");
				
				Console.WriteLine("\r\n\r\nEXE SourceContentFile FromString ToString");
				Console.WriteLine("Eg: EXE ABCDEFGH CD 12");
				Console.WriteLine("    Result: AB12EFGH");
				Console.Read();
			}
			
		}
		
		public static string convertSpecialChar(string content){
			return content.Replace("\\r","\r").Replace("\\n","\n").Replace("\\t","\t");
		}
		
		public static string readFile(string file){
			if(File.Exists(file)){
				StreamReader sr = new StreamReader(file);
				string content = sr.ReadToEnd();
				sr.Close();
				return content;
			}
			return "";
		}
		
		
		public static void writeFile(string file, string content){			
			StreamWriter sw = new StreamWriter(file);
			sw.Write(content);
			sw.Flush();
			sw.Close();			
		}
		
		//get string from string A, to string B
		//12345678 --> get from 23 to 78 --> 2345678
		public static string subString(string src, string from, string to, int fromIndex){
			string result = "";		
			
			if(src.IndexOf(from,fromIndex) >= 0 ){
				int startIndex = src.IndexOf(from,fromIndex);
				int endIndex   = src.IndexOf(to, startIndex + from.Length);
				int length	   = endIndex - startIndex + to.Length;
				if(startIndex >= 0 && length > 0)
				result = src.Substring( startIndex, length);
			}
			return result;			
		}
		
		//get string from string A, to string B but dont have A and B
		//12345678 --> get from 23 to 78 --> 456
		public static string subStringBetween(string src, string from, string to, int fromIndex){
			return subString(src,from,to,fromIndex).Replace(from,"").Replace(to,"");
		}
	}
}
